﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shop.Intermediate;
using Shop.Intermediate.Data;
using Shop.Core;
using DoNet.Common.Utilities;

namespace Shop.WebApp.SysBase
{
    public partial class BasicTypeManage : BasePage
    {
        private BasicTypeCore m_basicTypeCore;
        private int m_pageCount, m_recordCount;
        private const int PAGE_SIZE = 3;
        private Shop.WebApp.UserControl.SplitPage<IBasicType> spControl;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                m_basicTypeCore = new BasicTypeCore();
                spControl = new Shop.WebApp.UserControl.SplitPage<IBasicType>();
                pnPage.Controls.Add(spControl);

                if (!Page.IsPostBack)
                {
                    gvBaseTypeList.DataSource = m_basicTypeCore.PageResultQuery<IBasicType>(null, null
                        , null, null, 1, PAGE_SIZE, out m_pageCount, out m_recordCount);
                    gvBaseTypeList.DataBind();

                    spControl.Condition = null;
                    spControl.ConditionValue = null;
                    spControl.Orderby = null;
                    spControl.OrderbyValue = null;
                    spControl.PageCount = m_pageCount;
                    spControl.PageSize = PAGE_SIZE;
                    spControl.RecordCount = m_recordCount;
                    spControl.CurrentPageNo = 1;
                    spControl.Container = "gvBaseTypeList";
                    spControl.PageSplit = m_basicTypeCore;
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                IBasicType entity = m_basicTypeCore.NewBasicType();
                FormHelper<IBasicType>.Instance.UpdateModel(form1, entity);
                string message = string.Empty;
                if (m_basicTypeCore.Save(entity, ref message))
                {
                    MessageBox.Show(this.Page, message, "BasicTypeManage.aspx");
                    return;
                }
                else
                {
                    MessageBox.Show(this.Page, message);
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void btnUpadate_Click(object sender, EventArgs e)
        {
            try
            {
                IBasicType entity = m_basicTypeCore.NewBasicType();
                FormHelper<IBasicType>.Instance.UpdateModel(form1, entity);
                string message = string.Empty;
                if (m_basicTypeCore.Update(entity, ref message))
                {
                    MessageBox.Show(this.Page, message, "BasicTypeManage.aspx");
                    return;
                }
                else
                {
                    MessageBox.Show(this.Page, message);
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                string code = txtCode.Text.Trim();
                string name = txtName.Text.Trim();
                string whereStr = string.Empty;
                List<object> paramList = new List<object>();

                if(!string.IsNullOrEmpty(code))
                {
                    whereStr = "Code.Contains(@0)";
                    
                    paramList.Add(code);
                }
                
                if(!string.IsNullOrEmpty(name))
                {
                    if(whereStr.Length>1)
                        whereStr =whereStr + " && ";
                    whereStr = whereStr +"Name.Contains(@"+paramList.Count+")";
                    paramList.Add(name);
                }
                gvBaseTypeList.DataSource = m_basicTypeCore.PageResultQuery<IBasicType>(whereStr, paramList.ToArray()
                    , null, null, 1, PAGE_SIZE, out m_pageCount,out m_recordCount);
                gvBaseTypeList.DataBind();

                spControl.Condition = whereStr;
                spControl.ConditionValue = paramList.ToArray();
                spControl.Orderby = null;
                spControl.OrderbyValue = null;
                spControl.PageCount = m_pageCount;
                spControl.PageSize = PAGE_SIZE;
                spControl.RecordCount = m_recordCount;
                spControl.CurrentPageNo = 1;
                spControl.Container = "gvBaseTypeList";
                spControl.PageSplit = m_basicTypeCore;
                spControl.RefreshPageNo();
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void gvBaseTypeList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            try
            {
                string code = gvBaseTypeList.DataKeys[e.RowIndex].Values[0].ToString();
                string message = string.Empty;
                if (m_basicTypeCore.Delete(code, ref message))
                {
                    MessageBox.Show(this.Page, message, Request.RawUrl);
                    return;
                }
                else
                {
                    MessageBox.Show(this.Page, message);
                }
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
        }

        protected void gvBaseTypeList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            try
            {
                string code = gvBaseTypeList.DataKeys[e.NewSelectedIndex].Values[0].ToString();
                string message = string.Empty;
                IBasicType entity = m_basicTypeCore.GetEntity(code);

                this.Code.Text = entity.Code;
                this.Code.ReadOnly = true;
                this.btnSave.Visible = false;
                this.btnUpadate.Visible = true;

                this.Name.Text = entity.Name;
            }
            catch (Exception ex)
            {
                ProcessUnhandledException(ex);
            }
            
        }

        protected void lkbCreate_Click(object sender, EventArgs e)
        {
            this.btnSave.Visible = true;
            this.btnUpadate.Visible = false;
            this.Code.Text = string.Empty;
            this.Code.ReadOnly = false;
            this.Name.Text = string.Empty;
        }
    }
}
